package info.matsumana.sandbox;

import com.mysql.fabric.jdbc.FabricMySQLConnection;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Fabricサーバに接続してデータを表示するツール
 *
 * @author matsumana
 */
public class Select extends DatabaseAccess {

    public static void query(ResultSet result) throws SQLException {
        while (result.next()) {
            int id = result.getInt(1);
            int column_2 = result.getInt(2);
            Date column_3 = result.getDate(3);
            String column_4 = result.getString(4);

            System.out.println("id=" + id + ";column_2=" + column_2 + ";column_3=" + column_3 + ";column_4=" + column_4);
        }
    }

    public static void main(String... args) {

        String a_sql = "SELECT * FROM a";
        String b_sql = "SELECT * FROM b WHERE column_2 = ?";
        String c_sql = "SELECT * FROM c WHERE column_2 = ?";

        FabricMySQLConnection conn = null;
        try {
            conn = getConnection();
            PreparedStatement ps = conn.prepareStatement(a_sql);
            ResultSet result = ps.executeQuery();
            query(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
